//
//  ViewController.m
//  iOS页面逆向传值Demo
//
//  Created by 夏阳光 on 16/4/12.
//  Copyright © 2016年 夏阳光. All rights reserved.
//

#import "ViewController.h"
#import "NextViewController.h"

@interface ViewController ()<ChangeName>
@property (weak, nonatomic) IBOutlet UILabel *NameLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

//协议要实现的方法
-(void)changeName:(NSString *)string
{
    self.NameLabel.text=string;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    //    TwoViewController* vc=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TwoViewController"];
//
    NextViewController* vc=segue.destinationViewController;
    vc.delegate=self;
    
    
//block 写法1
    __weak __typeof(self) weakSelf = self;
    vc.change=^(NSString* stringName){
        weakSelf.NameLabel.text=stringName;
    };
    
//block 写法2
    [vc ChangeName:^(NSString *stringName) {
        self.NameLabel.text=stringName;
    }];
    
//通知中心添加观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"改变名称" object:nil];
}

//观察者接到消息之后执行的方法
-(void)changeLabelText:(NSNotification*)sender
{
    self.NameLabel.text=sender.userInfo[@"名字"];
}

//移除观察者
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}







@end
